<template>
  <div class="content-block block-half-1" v-loading="loading">
    <div class="info-head">{{ title }}</div>
    <div class="info-content">
      <v-chart :option="chartOption" style="width: 100%;height: 400px;"></v-chart>
    </div>
  </div>
</template>

<script>
import { createMonthRangeOfYear, uuid } from '@/utils'

export default {
  name: 'grid-item-two',
  props: {
    title: String,
  },
  data () {
    return {
      loading: false,
      info: []
    }
  },
  computed: {
    energyVariety () {
      return this.$store.state.energyVariety
    },
    chartOption () {
      const energyVariety = this.energyVariety
      return {
        legend: {
          left: 'center',
          data: energyVariety.map(v => v.name)
        },
        tooltip: {
          trigger: 'item'
        },
        grid: {
          left: '8%',
          right: '8%',
          bottom: 60
        },
        xAxis: {
          type: 'category',
          data: createMonthRangeOfYear(),
        },
        yAxis: {
          type: 'value',
          name: '吨标准煤'
        },
        series: [
          ...this.energyVariety.map((v) => ({
            name: v.name,
            type: 'line',
            smooth: true,
            data: this.info.map(vv => +vv[v.value])
          }))
        ]
      }
    }
  },
  mounted () {
    this.$store.dispatch('GET_ENERGY_VARIETY')
    this.query()
  },
  methods: {
    query () {
      this.loading = true
      // TODO
      const rand = (start = 0, end = 10) => Math.random() * (end - start)
      return new Promise(resolve => {
        setTimeout(() => {
          resolve()
          this.info = Array(12).fill(1).map(v => ({
            id: uuid(),
            water: rand(),
            electric: rand(),
            gas: rand(),
            other: rand(),
          }))
        }, 200)
      }).finally(() => {
        this.loading = false
      })
    },
  },
}
</script>

<style scoped>

</style>
